/***************************************************************************
 *   This file is part of KTagger                                          *
 *   Copyright (C) 2010 by Luca Bellonda                                   *
 *   lbellonda _at_ gmail.com                                              *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "KTaggerAppletPrivate.h"
#include "applet/config-applet.h"
#include <QAction>
#include <Plasma/Context>

#define SIZE_CMIN 16
#define SIZE_CPREF 32
#define SIZE_CMAX 64
#define SPACING 8

#define ICON_PROFILE    "ktagger1"
#define ICON_OTHERTAGS  "ktagger2"
#define ICON_WORK       "user-away"

#define SOURCE_CLASS "KTaggerApplet::Private"


KTaggerApplet::Private::Private(KTaggerApplet * parent)
        : _owner(parent)
{
    _activityLabel = NULL;
    _inited = false;
    _state = AVAILABLE ;
    initLog();
    //_mainApplication = new org::kde::ktagger::App( "org.kde.plasmatag", "/PlasmaTag", QDBusConnection::sessionBus() );
    connect(&_workerThread, SIGNAL(processFinished(const bool, const int, const QString &)), this, SLOT(assignDone(const bool , const int , const QString &)));
    connect(&_workerThread, SIGNAL(processProfileFinished(const bool, const int, const QString &)), this, SLOT(assignProfileDone(const bool , const int , const QString &)));
    connect(&_workerThread, SIGNAL(configurationFinished()), this, SLOT(configurationDone()));
}

KTaggerApplet::Private::~Private()
{
    if (NULL != _assignTagProfileIcon) {
        _assignTagProfileIcon->disconnect(SIGNAL(droppedFiles(const QStringList &)));
    }
    if (NULL != _assignOtherTagsIcon) {
#ifdef  _DEBUG_ACTION
        _assignOtherTagsIcon->disconnect(SIGNAL(clicked()));
#endif
        _assignOtherTagsIcon->disconnect(SIGNAL(droppedFiles(const QStringList &)));
    }
}



void KTaggerApplet::Private::init()
{
    if (_inited) {
        return ;
    }
    _inited = true;
    _layout = new QGraphicsLinearLayout();
    _layout->setSpacing(0.0);
    _layout->setOrientation(Qt::Horizontal);

    int buttonsize = KIconLoader::SizeMedium ;
    _assignTagProfileIcon = new KTagIcon();
    _assignTagProfileIcon->setIcon(KIcon(ICON_PROFILE));
    _assignTagProfileIcon->setText(i18n("Profile"));
    _assignTagProfileIcon->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    _assignTagProfileIcon->setOrientation(Qt::Vertical);
    _assignTagProfileIcon->setAcceptDrops(true);
    connect(_assignTagProfileIcon, SIGNAL(droppedFiles(const QStringList &)), this, SLOT(droppedFiles(const QStringList &)));
    _layout->addItem(_assignTagProfileIcon);

    _assignOtherTagsIcon = new KTagIcon();
    _assignOtherTagsIcon->setText(i18n("Tags..."));
    _assignOtherTagsIcon->setIcon(KIcon(ICON_OTHERTAGS));
    _assignOtherTagsIcon->setAcceptDrops(true);
    _assignOtherTagsIcon->setOrientation(Qt::Vertical);
    _assignOtherTagsIcon->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
#ifdef  _DEBUG_ACTION
    connect(_assignOtherTagsIcon, SIGNAL(clicked()), this, SLOT(assignClicked()));
#endif
    connect(_assignOtherTagsIcon, SIGNAL(droppedFiles(const QStringList &)), this, SLOT(droppedFiles2(const QStringList &)));
    _layout->addItem(_assignOtherTagsIcon);

#ifdef  DEBUG_MYAPPLET
    QGraphicsLinearLayout *topLayout = new QGraphicsLinearLayout();
    topLayout->setSpacing(0.0);
    topLayout->setOrientation(Qt::Vertical);
    _activityLabel = new Plasma::Label();
    _activityLabel->setAlignment(Qt::AlignHCenter);
    topLayout->addItem(_activityLabel);
    topLayout->addItem(_layout);
    _owner->setLayout(topLayout);
#else
    _owner->setLayout(_layout);
#endif

    QAction* configureAction = new QAction(i18n("Configure..."), this);
    _contextualActions.append(configureAction);
    connect(configureAction, SIGNAL(triggered(bool)), this, SLOT(doConfigure()));
    QAction* chooseProfileAction = new QAction(i18n("Choose Profile..."), this);
    _contextualActions.append(chooseProfileAction);
    connect(chooseProfileAction , SIGNAL(triggered(bool)), this, SLOT(doChooseProfile()));

}


void KTaggerApplet::Private::assignClicked()
{
#ifdef  _DEBUG_ACTION
    logger.debug(&_logParams, "assignClicked()");
    if (AVAILABLE == _state) {
        _state = TAGGING ;
        _assignOtherTagsIcon->setIcon(KIcon(ICON_WORK));
        _workerThread.callDebug();
    }
#endif
}

void KTaggerApplet::Private::droppedFiles(const QStringList & fileList)
{
    logger.debug(&_logParams, "dropppedFiles()");
    if (AVAILABLE == _state) {
        _state = TAGGING ;
        _assignTagProfileIcon->setIcon(KIcon(ICON_WORK));
        _workerThread.assignFiles(_owner->context()->currentActivity(), fileList);
    }
}

void KTaggerApplet::Private::droppedFiles2(const QStringList & fileList)
{
    logger.debug(&_logParams, "dropppedFiles2()");
    if (AVAILABLE == _state) {
        _state = TAGGING ;
        _assignOtherTagsIcon->setIcon(KIcon(ICON_WORK));
        _workerThread.assignOtherTags(/*TODO */ "", fileList.at(0));
    }
}


bool KTaggerApplet::Private::canDoOperation()
{
    return (AVAILABLE == _state);
}

void KTaggerApplet::Private::assignDone(const bool isError, const int errorCode, const QString &errorMessage)
{
    logger.debug(&_logParams, "assignDone()");
    _assignOtherTagsIcon->setIcon(KIcon(ICON_OTHERTAGS));
    _state = AVAILABLE ;
}

void KTaggerApplet::Private::assignProfileDone(const bool isError, const int errorCode, const QString &errorMessage)
{
    logger.debug(&_logParams, "assignDone()");
    _assignTagProfileIcon->setIcon(KIcon(ICON_PROFILE));
    _state = AVAILABLE ;
}

void KTaggerApplet::Private::initLog()
{
    _logParams.source = SOURCE_CLASS;
}

QList<QAction*> KTaggerApplet::Private::getContextualActions()
{
    return _contextualActions;
}

//TODO: abilita e disabilita
void KTaggerApplet::Private::doConfigure()
{
    logger.debug(&_logParams, "configure()");
    if (AVAILABLE == _state) {
        _state = CONFIGURING ;
        _assignOtherTagsIcon->setIcon(KIcon(ICON_WORK));
        _assignTagProfileIcon->setIcon(KIcon(ICON_WORK));
        _owner->setBusy(true);
        _workerThread.configure();
    }
}

void KTaggerApplet::Private::configurationDone()
{
    logger.debug(&_logParams, "configurationDone()");
    _assignOtherTagsIcon->setIcon(KIcon(ICON_OTHERTAGS));
    _assignTagProfileIcon->setIcon(KIcon(ICON_PROFILE));
    _state = AVAILABLE ;
    _owner->setBusy(false);
}

void KTaggerApplet::Private::doChooseProfile()
{
    logger.debug(&_logParams, "chooseProfile()");
    if (AVAILABLE == _state) {
        _state = CHOOSING ;
        _assignOtherTagsIcon->setIcon(KIcon(ICON_WORK));
        _assignTagProfileIcon->setIcon(KIcon(ICON_WORK));
        _owner->setBusy(true);
        _workerThread.chooseProfile();
    }
}


void KTaggerApplet::Private::setCurrentActivity(const QString &activityName)
{
    if (logger.isLoggable(TLogger::DEBUG)) {
        logger.debug(&_logParams, QString("setCurrentActivity ='%1'").arg(activityName));
    }
    if (NULL != _activityLabel) {
        _activityLabel->setText(activityName);
    }
    _currentActivity = activityName ;
    QStringList tags = _workerThread.callGetTags(_currentActivity);

    bool isEnabled = false ;
    if (tags.length() > 0) {
        isEnabled = true ;
    }
    _assignTagProfileIcon->setEnabled(isEnabled);
}
